package com.hotel.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.auto.JSONAuthentication;
import com.hotel.entity.HoHouse;
import com.hotel.entity.HoUser;
import com.hotel.mapper.HoHouseMapper;
import com.hotel.mapper.HoPassMapper;
import com.hotel.mapper.HoUserMapper;
import com.hotel.mapper.HouseTypeMapper;
import com.hotel.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/ho-house")
public class HoHouseController extends JSONAuthentication {
    @Autowired
    HoHouseMapper hoHouseMapper;
    @Autowired
    HoPassMapper hoPassMapper;
    @Autowired
    HouseTypeMapper houseTypeMapper;
    @ApiOperation(value = "添加房间")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("type") int type) throws Exception{
        HoHouse hoHouse=new HoHouse();
        hoHouse.setType(type);

        int i=hoHouseMapper.insert(hoHouse);

        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "删除房间")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=hoHouseMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找所有房间")
    @PostMapping("/findAllHouse")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<HoHouse> shUserPage=new Page<>(current,size);
        Page<HoHouse> shUserPages=hoHouseMapper.selectPage(shUserPage,null);
        List<HoHouse> list = shUserPages.getRecords();
        int total=hoHouseMapper.selectCount(null);
        for(HoHouse a:list){
            if(a.getLkId()!=0){
                a.setHoPass(hoPassMapper.selectById(a.getLkId()));
            }
            a.setHouseType(houseTypeMapper.selectById(a.getType()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过房间id查找房间")
    @PostMapping("/lookHouseByid")
    public void updUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id)throws Exception{
        HoHouse shDress=hoHouseMapper.selectById(id);
        if(shDress.getLkId()!=0){
            shDress.setHoPass(hoPassMapper.selectById(shDress.getLkId()));
        }
        shDress.setHouseType(houseTypeMapper.selectById(shDress.getType()));
        if(shDress!=null){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("房间不存在");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过房间id修改房间状态")
    @PostMapping("/updHouseByid")
    public void updUsera(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status") int status)throws Exception{
        HoHouse shDress=hoHouseMapper.selectById(id);
        shDress.setStatus(status);

        if(hoHouseMapper.updateById(shDress)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("修改失败");
            this.WriteJSON(request, response, error);
        }
    }
}

